#include "chatwidget.h"
#include "ui_chatwidget.h"
#include "Views/senderwidget.h"
#include "Views/receivewidget.h"

ChatWidget::ChatWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatWidget)
{
    ui->setupUi(this);
}

ChatWidget::~ChatWidget()
{
    delete ui;
}

void ChatWidget::setData(Friend *data)
{
    ui->listWidget->clear();
    ui->nameLbl->setText(data->name());
    auto messages = data->messages();
    for (int i = 0; i < messages.count(); ++i) {
        auto message = messages[i];
        auto item = new QListWidgetItem;
        ui->listWidget->addItem(item);
        if (message.msgType() == MsgType::Sender) {
            auto widget = new SenderWidget;
            ui->listWidget->setItemWidget(item, widget);
            connect(widget, &SenderWidget::sizeChanged, [=](QRect rect) mutable {
                item->setSizeHint(QSize(width() * 3 / 5, rect.height() + 20));
            });
            widget->setMessage(message.content());
        } else if (message.msgType() == MsgType::Receive) {
            auto widget = new ReceiveWidget;
            ui->listWidget->setItemWidget(item, widget);
            connect(widget, &ReceiveWidget::sizeChanged, [=](QRect rect) mutable {
                item->setSizeHint(QSize(width() * 3 / 5, rect.height() + 20));
            });
            widget->setMessage(message.content());
        }

    }
}
